<?php

namespace Admin\Models\sys;

use Helpers\Cache;
use Admin\Models\BaseModel;

class XhsysEnum extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iEnumId;

    /**
     *
     * @var integer
     */
    public $iNodeId;

    /**
     *
     * @var string
     */
    public $sEnumName;

    /**
     *
     * @var string
     */
    public $sEnumValue;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("'XHSys_Enum'");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSys_Enum';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysEnum[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysEnum
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据id及数据增加或修改数据并返回
     *
     * @param array $data
     * @return XhsysEnum
     *
     * create by ck 201600617
     * modify by ck 20161014
     */
    public static function createRecord($data = array())
    {
        $obj = isset($data['iEnumId']) ? self::findFirst($data['iEnumId']) : new self;
        $obj or $obj = new self;
        $obj->assign($data);
        return $obj;
    }

    /**
     * 根据节点ID返回枚举列表
     *
     * @param $nodeId
     * @return bool|array
     *
     * add by ck 20161008
     */
    public static function getEnumByNodeId($nodeId)
    {
        if(!$nodeId) return [];
        if(Cache::exists('enum_id_' . $nodeId)) {
            return Cache::get('enum_id_' . $nodeId);
        }
        $enumList = self::find([
            'conditions' => "iNodeId = {$nodeId} AND bDelete = 0",
            'columns'    => 'sEnumValue,sEnumName'
        ])->toArray();
        if(!$enumList) {
            Cache::save('enum_name_' . $nodeId, []);
            return [];
        }
        $enumList = array_column($enumList, 'sEnumName', 'sEnumValue');
        Cache::save('enum_id_' . $nodeId, $enumList);
        return $enumList;
    }

    /**
     * 根据枚举名称获取枚举列表
     * 先找出节点id，再调用方法查找枚举列表
     *
     * @param $nodeName
     * @return array|bool
     *
     * add by ck 20161008
     */
    public static function getEnumByNodeName($nodeName)
    {
        if(!$nodeName) return [];
        if(Cache::exists('enum_name_' . $nodeName)) {
            return Cache::get('enum_name_' . $nodeName);
        }
        $node = XhsysTree::findFirst([
            'conditions' => "sNodeName = '{$nodeName}' AND iNodeType = 2 AND bDelete = 0"
        ]);
        if(!$node) {
            Cache::save('enum_name_' . $nodeName, []);
            return [];
        }
        $nodeId = $node->iNodeId;
        $enumList = self::getEnumByNodeId($nodeId);
        if(!$enumList) return [];
        Cache::save('enum_name_' . $nodeName, $enumList);
        return $enumList;
    }

}
